﻿using ET_DbCommon.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET_DbCommon.DAL
{
    /// <summary>
    /// 试卷任务表
    /// </summary>
    public static class Paper_TestQuestion_TaskDAL
    {
        /// <summary>
        /// 查找考试试卷任务
        /// </summary>
        /// <param name="Exam_TestQuestionId"></param>
        /// <returns></returns>
        public static List<Paper_TestQuestion_TaskModel> GetMyPaper_TestQuestion_TaskModel(int Paper_TestQuestionId)
        {
            List<Paper_TestQuestion_TaskModel> paper_TestQuestion_TaskModels = new List<Paper_TestQuestion_TaskModel>();
            string where = "where TestId={0}";
            where = string.Format(where, Paper_TestQuestionId);
            using (var conn = MySqlHelper.GetConnection())
            {
                paper_TestQuestion_TaskModels = MySQLExt.GetByWhere<Paper_TestQuestion_TaskModel>(conn, where).ToList();
                if (paper_TestQuestion_TaskModels == null)
                    return null;
                return paper_TestQuestion_TaskModels;
            }
        }

        /// <summary>
        /// 查找考试试卷任务
        /// </summary>
        /// <param name="Exam_TestQuestionId"></param>
        /// <returns></returns>
        public static Paper_TestQuestion_TaskModel GetByID(int id)
        {
            List<Paper_TestQuestion_TaskModel> paper_TestQuestion_TaskModels = new List<Paper_TestQuestion_TaskModel>();
            string where = "where Id={0}";
            where = string.Format(where, id);
            using (var conn = MySqlHelper.GetConnection())
            {
                paper_TestQuestion_TaskModels = MySQLExt.GetByWhere<Paper_TestQuestion_TaskModel>(conn, where).ToList();
                if (paper_TestQuestion_TaskModels == null || paper_TestQuestion_TaskModels.Count == 0)
                    return null;
                return paper_TestQuestion_TaskModels.First();
            }
        }

        public static int Update(Paper_TestQuestion_TaskModel paper_TestQuestion_TaskModel)
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                var excuteNum = MySQLExt.UpdateById(conn, paper_TestQuestion_TaskModel);
                return excuteNum;
            }
        }

        public static void Insert(List<Paper_TestQuestion_TaskModel> paper_TestQuestion_TaskModels)
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                foreach (var model in paper_TestQuestion_TaskModels)
                {
                    MySQLExt.InsertOrUpdate(conn, model);
                }
            }
        }

        public static int Insert(Paper_TestQuestion_TaskModel paper_TestQuestion_TaskModel)
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                return MySQLExt.InsertIdentityIndex(conn, paper_TestQuestion_TaskModel);
            }
        }

        public static int DeleteByID(int taskID)
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                int count = 0;
                MySQLExt.DeleteById<Paper_TestQuestion_TaskModel>(conn, taskID);
                var steps = Paper_TestQuestion_Task_StepDAL.GetMyPaper_TestQuestion_Task_StepModel(taskID);
                foreach (var step in steps)
                {
                    int excuteNum = Paper_TestQuestion_Task_StepDAL.DeleteByID(step.Id);
                    count += excuteNum;
                }
                return count;
            }
        }


    }
}
